package com.borball.store.util;

import java.util.List;

import com.borball.store.dao.UserDAO;
import com.borball.store.domain.User;

import craky.security.Digest;

public class UserUtils {

	public final static int SUCCESS = 0;
	public final static int USER_NOT_EXIST = 1;
	public final static int WRONG_PASSWORD = 2;
    public static final int ADMIN = 0;
    public static final int USER = 10;
    public static final int VISITOR = 20;
    public static final int MIN_PASSWORD_LENGTH = 4;
    public static final int MAX_PASSWORD_LENGTH = 100;
    public static final String ADMIN_DES = "\u8D85\u7EA7\u7BA1\u7406\u5458";
    public static final String USER_DES = "\u4E00\u822C\u7528\u6237";
    public static final String VISITOR_DES = "\u53D7\u9650\u53C2\u89C2\u8005";
    public static final String USER_NOT_EXISTS = "\u8BE5\u7528\u6237\u4E0D\u5B58\u5728\uFF01";
    public static final String USER_ALREADY_EXIST = "\u8BE5\u7528\u6237\u5DF2\u5B58\u5728\uFF01";
    public static final String ILLEGAL_PASSWORD = "\u5BC6\u7801\u9519\u8BEF\uFF01";
    public static final String NAME_UNALLOWED = "\u975E\u6CD5\u7528\u6237\u540D\uFF01";
    public static final String PASSWORD_LENGTH_ERROR = "\u5BC6\u7801\u957F\u5EA6\u4E0D\u80FD\u5C0F\u4E8E" +
                    MIN_PASSWORD_LENGTH + "\u4F4D\uFF01";
    public static final String NAME_REGEX_INFO = "\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A\u4E14\u4E0D\u80FD" +
    		        "\u51FA\u73B0\u534A\u89D2\u9017\u53F7(,)\uFF01";
    
    private List<User> users = null;
    private static UserUtils  userUtils = null;

    private  UserUtils(){
    	loadUsers();
    }
    
    private void loadUsers() {
    	users = findAll();
	}
    
    private void reloadUsers() {
    	users = findAll();
	}

	public static UserUtils getInstance(){
    	if(userUtils == null)
    		userUtils = new UserUtils();
    	return userUtils;
    }
    
	public boolean isEmptyUser() {
		return users.isEmpty();
	}

	public int isValidUser(String name, String password) {
		UserDAO dao = new UserDAO();
		User u = dao.findByPK(name);

		if (u == null)
			return USER_NOT_EXIST;
		if (password.equals(u.getPassword()))
			return SUCCESS;
		else
			return WRONG_PASSWORD;
	}
	
	public boolean isExisting(String name) {
		UserDAO dao = new UserDAO();
		User u = dao.findByPK(name);

		if (u == null)
			return false;
		else 
			return true;
			
	}
	
	public boolean isFirstUse() {
		return isEmptyUser();
	}
	
	public boolean isAllowedName(String name) {
		return name != null && !(name = name.trim()).isEmpty() && name.indexOf(',') < 0;
	}

	public boolean isAllowedPassword(String password) {
		return password.length() >= MIN_PASSWORD_LENGTH;
	}
	
	public User login(String name, String password) {
		UserDAO dao = new UserDAO();
		User user = dao.findByPK(name);
		if (user != null && !user.getPassword().equals(password)) {
			user.setPassword(null);
		}
		return user;
	}
	
	public void addUser(User user){
		UserDAO dao = new UserDAO();
		dao.add(user);
		
		reloadUsers();
	}
	
	public void updateUser(User user){
		UserDAO dao = new UserDAO();
		dao.update(user);
		
		reloadUsers();
	}
	
	
	public void deleteUser(User user){
		UserDAO dao = new UserDAO();
		dao.delete(user);
		
		reloadUsers();
	}
	

	public String createCiphertext(String name, String password) {
		return Digest.computeMD5(Digest.computeMD5(name) + Digest.computeMD5(password));
	}
	
	public  List<User> getAllUsers(){
		return users;
	}
	
	private List<User> findAll(){
		UserDAO dao = new UserDAO();
		return dao.findAll();
	}
	
	public User findByName(String name) {
		UserDAO dao = new UserDAO();
		return dao.findByPK(name);
	}
	
	public void updatePassword(User user){
		UserDAO dao = new UserDAO();
		dao.updatePassword(user);

	}
}
